<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\service\MessageService;
use Webman\Stomp\RetryAbleConsumer;

class SendMessageConsumer extends BaseConsumer implements RetryAbleConsumer
{

    // 要消费的队列名
    public $queue = 'send_message';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     */
    public function doConsume($data)
    {
        $userService = new MessageService();
        $sendingData = [$data['message_id']];
        //判断是否存在
        $notifierRestricted = $data['notifier_restricted'] ?? [];
        if ($notifierRestricted) {
            $sendingData[] = $data['notifier_restricted'];
        }
        $userService->sendMessageAsync(...$sendingData);
    }

    /**
     * 重试间隔
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * @param $attempt
     * @param $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        return $attempt < 3;
    }

}
